<?php

$today = $_GET['today'];
$openid = $_GET['openid'];
$conn = mysqli_connect("localhost", "root", "", "cloudwarehouse");

if (!$conn)
    echo "数据库连接失败！";
else {
    $sixdays = array(6);
    $sixdays_profits = array(7);

    $sql_today = "SELECT sum(profit) FROM commodity_profit WHERE outboundDate = '$today' AND openid = '$openid'";
        $result_gettoday = mysqli_query($conn, $sql_today);
        $profit_todaypre = mysqli_fetch_array($result_gettoday);
                                                                        //今天利润
        if ($profit_todaypre[0] == NULL) {
            $profit_today = 0;
        } else {
            $profit_today = round($profit_todaypre[0], 2);
        }
        $sixdays_profits[0] = $profit_today;


    for ($i = 0; $i < 6; $i++) {
        $j = $i + 1;
        $sixdays[$i] = Date("Y-m-d", strtotime("-$j day" . $today));

        $sql_search = "SELECT sum(profit) FROM commodity_profit WHERE outboundDate = '$sixdays[$i]' AND openid = '$openid'";
        $result_getprofit = mysqli_query($conn, $sql_search);

        $profit_pre = mysqli_fetch_array($result_getprofit);
        //  echo $profit_pre[0];                 //利润
        if ($profit_pre[0] == NULL) {
            $profit = 0;
        } else {
            $profit = round($profit_pre[0], 2);
        }
        $sixdays_profits[$i+1] = $profit;
    }
    echo json_encode(array_reverse($sixdays_profits));
}
